package com.cloudeasy.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudeasy.api.enums.ResultCodeEnum;
import com.cloudeasy.api.exception.CloudEasyException;
import com.cloudeasy.user.mapper.AttentionMapper;
import com.cloudeasy.user.po.Attention;
import com.cloudeasy.user.po.User;
import com.cloudeasy.user.util.IPageUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AttentionService extends ServiceImpl<AttentionMapper, Attention> {

    private final UserService userService;

    private final UserInfoService userInfoService;

    public AttentionService(@Lazy UserService userService, @Lazy UserInfoService userInfoService) {
        this.userService = userService;
        this.userInfoService = userInfoService;
    }

    /**
     * 根据用户Id查询记录
     *
     * @param userId 用户Id
     * @return {@link List <Attention>}
     */
    public List<Attention> listByUserId(String userId) {
        QueryWrapper<Attention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.list(queryWrapper);
    }

    /**
     * 根据用户Id分页查询记录
     *
     * @param userId 用户Id
     * @param page   页数
     * @param size   每页大小
     * @return {@link IPage<Attention>}
     */
    public IPage<Attention> pageByUserId(String userId, Integer page, Integer size) {
        QueryWrapper<Attention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        IPage<Attention> attentionIPage = IPageUtil.compose(page, size);
        return this.page(attentionIPage, queryWrapper);
    }

    /**
     * 根据被关注人Id查询记录
     *
     * @param id Id
     * @return {@link List<Attention>}
     */
    public List<Attention> listById(String id) {
        QueryWrapper<Attention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attention_id", id);
        return this.list(queryWrapper);
    }

    /**
     * 根据被关注人Id分页查询记录
     *
     * @param id   Id
     * @param page 页数
     * @param size 每页大小
     * @return {@link IPage<Attention>}
     */
    public IPage<Attention> pageById(String id, Integer page, Integer size) {
        QueryWrapper<Attention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attention_id", id);
        IPage<Attention> attentionIPage = IPageUtil.compose(page, size);
        return this.page(attentionIPage, queryWrapper);
    }

    /**
     * 关注
     *
     * @param userId 用户Id
     * @param id     Id
     * @return {@link Boolean}
     */
    public Boolean attention(String userId, String id) {
        User user = userService.getById(id);
        if (user == null) {
            throw new CloudEasyException(ResultCodeEnum.ID_NOT_EXISTS);
        }
        if (this.getOne(wrapper(userId, id)) != null) {
            throw new CloudEasyException(ResultCodeEnum.REPEAT_ERROR);
        }
        Attention attention = Attention.builder().userId(userId).attentionId(id).build();

        userInfoService.attention(userId, id);
        return this.save(attention);
    }

    /**
     * 取消关注
     *
     * @param userId 用户Id
     * @param id     Id
     * @return {@link Boolean}
     */
    public Boolean removeAttention(String userId, String id) {
        if (this.getOne(wrapper(userId, id)) == null) {
            throw new CloudEasyException(ResultCodeEnum.REPEAT_ERROR);
        }

        userInfoService.removeAttention(userId, id);
        return this.remove(wrapper(userId, id));
    }

    /**
     * 构建wrapper
     *
     * @param userId 用户Id
     * @param id     Id
     * @return {@link QueryWrapper<Attention>}
     */
    public QueryWrapper<Attention> wrapper(String userId, String id) {
        QueryWrapper<Attention> queryWrapper = new QueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq("user_id", userId);
        }
        if (id != null) {
            queryWrapper.eq("attention_id", id);
        }
        return queryWrapper;
    }
}
